package com.nenglai.common.resource;

import com.kordar.datalist.DatalistService;
import com.nenglai.common.vo.SearchVO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * Datalist Aspect
 *
 * @author kongdehui
 * @date 2021/04/27 20:00
 **/
@Aspect
@Component
public class DatalistAspect {

    @Resource
    DatalistService datalistService;

    @AfterReturning(returning="rvt", pointcut="@annotation(Datalist)")
    public Object AfterExec(JoinPoint joinPoint, Object rvt){

        // 获取注解中的参数值
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        // 获取注解Action
        Datalist annotation = method.getAnnotation(Datalist.class);
        // 获取注解Action的value参数的值
        Class<?> entity = annotation.entity();
        Class<?> vo = annotation.vo();

        SearchVO searchVO = (SearchVO) rvt;
        searchVO.setData(datalistService.renderVo(searchVO.getData(), entity, vo));
        return searchVO;
    }
}
